home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / zip19p1.zoo / zipfile.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  25KB  |  801 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included
  7.  unmodified, that it is not sold for profit, and that this copyright notice
  8.  is retained.
  9.  
  10. */
  11.  
  12. /*
  13.  *  zipfile.c by Mark Adler.
  14.  */
  15.  
  16. #include "zip.h"
  17.  
  18. #ifdef VMS
  19. #  include "VMSmunch.h"
  20. #  include <rms.h>
  21. #endif
  22.  
  23.  
  24. /* Macros for converting integers in little-endian to machine format */
  25. #define SH(a) (((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8))
  26. #define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
  27.  
  28. /* Macros for writing machine integers to little-endian format */
  29. #define PUTSH(a,f) {putc((char)(a),(f)); putc((char)((a) >> 8),(f));}
  30. #define PUTLG(a,f) {PUTSH(a,f) PUTSH((a) >> 16,f)}
  31.  
  32.  
  33. /* -- Structure of a ZIP file -- */
  34.  
  35. /* Signatures for zip file information headers */
  36. #define LOCSIG     0x04034b50L
  37. #define CENSIG     0x02014b50L
  38. #define ENDSIG     0x06054b50L
  39. #define EXTLOCSIG  0x08074b50L
  40.  
  41. /* Offsets of values in headers */
  42. #define LOCVER  0               /* version needed to extract */
  43. #define LOCFLG  2               /* encrypt, deflate flags */
  44. #define LOCHOW  4               /* compression method */
  45. #define LOCTIM  6               /* last modified file time, DOS format */
  46. #define LOCDAT  8               /* last modified file date, DOS format */
  47. #define LOCCRC  10              /* uncompressed crc-32 for file */
  48. #define LOCSIZ  14              /* compressed size in zip file */
  49. #define LOCLEN  18              /* uncompressed size */
  50. #define LOCNAM  22              /* length of filename */
  51. #define LOCEXT  24              /* length of extra field */
  52.  
  53. #define CENVEM  0               /* version made by */
  54. #define CENVER  2               /* version needed to extract */
  55. #define CENFLG  4               /* encrypt, deflate flags */
  56. #define CENHOW  6               /* compression method */
  57. #define CENTIM  8               /* last modified file time, DOS format */
  58. #define CENDAT  10              /* last modified file date, DOS format */
  59. #define CENCRC  12              /* uncompressed crc-32 for file */
  60. #define CENSIZ  16              /* compressed size in zip file */
  61. #define CENLEN  20              /* uncompressed size */
  62. #define CENNAM  24              /* length of filename */
  63. #define CENEXT  26              /* length of extra field */
  64. #define CENCOM  28              /* file comment length */
  65. #define CENDSK  30              /* disk number start */
  66. #define CENATT  32              /* internal file attributes */
  67. #define CENATX  34              /* external file attributes */
  68. #define CENOFF  38              /* relative offset of local header */
  69.  
  70. #define ENDDSK  0               /* number of this disk */
  71. #define ENDBEG  2               /* number of the starting disk */
  72. #define ENDSUB  4               /* entries on this disk */
  73. #define ENDTOT  6               /* total number of entries */
  74. #define ENDSIZ  8               /* size of entire central directory */
  75. #define ENDOFF  12              /* offset of central on starting disk */
  76. #define ENDCOM  16              /* length of zip file comment */
  77.  
  78.  
  79. /* Local functions */
  80. #ifdef PROTO
  81.    local int zqcmp(voidp *, voidp *);
  82. #  ifndef UTIL
  83.      local int zbcmp(voidp *, voidp far *);
  84.      local char *cutpath(char *);
  85. #  endif /* !UTIL */
  86. #endif /* PROTO */
  87.  
  88.  
  89. local int zqcmp(a, b)
  90. voidp *a, *b;           /* pointers to pointers to zip entries */
  91. /* Used by qsort() to compare entries in the zfile list.  */
  92. {
  93.   return namecmp((*(struct zlist far **)a)->zname,
  94.                 (*(struct zlist far **)b)->zname);
  95. }
  96.  
  97.  
  98. #ifndef UTIL
  99.  
  100. local int zbcmp(n, z)
  101. voidp *n;               /* string to search for */
  102. voidp far *z;           /* pointer to a pointer to a zip entry */
  103. /* Used by search() to compare a target to an entry in the zfile list. */
  104. {
  105.   return namecmp((char *)n, ((struct zlist far *)z)->zname);
  106. }
  107.  
  108.  
  109. struct zlist far *zsearch(n)
  110. char *n;                /* name to find */
  111. /* Return a pointer to the entry in zfile with the name n, or NULL if
  112.    not found. */
  113. {
  114.   voidp far **p;        /* result of search() */
  115.  
  116.   if (zcount && (p = search(n, (voidp far **)zsort, zcount, zbcmp)) != NULL)
  117.     return *(struct zlist far **)p;
  118.   else
  119.     return NULL;
  120. }
  121.  
  122. #endif /* !UTIL */
  123.  
  124. #ifndef VMS
  125. #  define PATHCUT '/'
  126.  
  127. char *ziptyp(s)
  128. char *s;                /* file name to force to zip */
  129. /* If the file name *s has a dot (other than the first char), then return
  130.    the name, otherwise append .zip to the name.  Allocate the space for
  131.    the name in either case.  Return a pointer to the new name, or NULL
  132.    if malloc() fails. */
  133. {
  134.   char *q;              /* temporary pointer */
  135.   char *t;              /* pointer to malloc'ed string */
  136.  
  137.   if ((t = malloc(strlen(s) + 5)) == NULL)
  138.     return NULL;
  139.   strcpy(t, s);
  140. #ifdef MSDOS
  141.   for (q = t; *q; q++)
  142.     if (*q == '\\')
  143.       *q = '/';
  144. #endif /* MSDOS */
  145.   if (strrchr((q = strrchr(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
  146.     strcat(t, ".zip");
  147. #if defined(FORCE_UPPER) && defined(MSDOS) && !defined(OS2)
  148.   strupr(t);
  149. #endif
  150.   return t;
  151. }
  152.  
  153. #else /* VMS */
  154.  
  155. # define PATHCUT ']'
  156.  
  157. char *ziptyp(s)
  158. char *s;
  159. {   int status;
  160.     struct FAB fab;
  161.     struct NAM nam;
  162.     static char zero=0;
  163.     char result[NAM$C_MAXRSS+1],exp[NAM$C_MAXRSS+1];
  164.     char *p;
  165.  
  166.     fab = cc$rms_fab;
  167.     nam = cc$rms_nam;
  168.  
  169.     fab.fab$l_fna = s;
  170.     fab.fab$b_fns = strlen(fab.fab$l_fna);
  171.  
  172.     fab.fab$l_dna = "sys$disk:[].zip";          /* Default fspec */
  173.     fab.fab$b_dns = strlen(fab.fab$l_dna);
  174.  
  175.     fab.fab$l_nam = &nam;
  176.     
  177.     nam.nam$l_rsa = result;                     /* Put resultant name of */
  178.     nam.nam$b_rss = sizeof(result)-1;           /* existing zipfile here */
  179.  
  180.     nam.nam$l_esa = exp;                        /* For full spec of */
  181.     nam.nam$b_ess = sizeof(exp)-1;              /* file to create */
  182.  
  183.     status = sys$parse(&fab);
  184.     if( (status & 1) == 0 )
  185.         return &zero;
  186.  
  187.     status = sys$search(&fab);
  188.     if( status & 1 )
  189.     {               /* Existing ZIP file */
  190.         int l;
  191.         if( (p=malloc( (l=nam.nam$b_rsl) + 1 )) != NULL )
  192.         {       result[l] = 0;
  193.                 strcpy(p,result);
  194.         }
  195.     }
  196.     else
  197.     {               /* New ZIP file */
  198.         int l;
  199.         if( (p=malloc( (l=nam.nam$b_esl) + 1 )) != NULL )
  200.         {       exp[l] = 0;
  201.                 strcpy(p,exp);
  202.         }
  203.     }
  204.     return p;
  205. }
  206.  
  207. #endif  /* VMS */
  208.  
  209.  
  210. int readzipfile()
  211. /*
  212.    Make first pass through zip file, reading information from local file
  213.    headers and then verifying that information with the central file
  214.    headers.  Any deviation from the expected zip file format returns an
  215.    error.  At the end, a sorted list of file names in the zip file is made
  216.    to facilitate searching by name.
  217.  
  218.    The name of the zip file is pointed to by the global "zipfile".  The
  219.    globals zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  220.    Return an error code in the ZE_ class.
  221. */
  222. {
  223.   char b[CENHEAD];      /* buffer for central headers */
  224.   FILE *f;              /* zip file */
  225.   ush flg;              /* general purpose bit flag */
  226.   int m;                /* mismatch flag */
  227.   extent n;             /* length of name */
  228.   ulg p;                /* current file offset */
  229.   char r;               /* holds reserved bits during memcmp() */
  230.   ulg s;                /* size of data, start of central */
  231.   char *t;              /* temporary variable */
  232.   char far *u;          /* temporary variable */
  233.   struct zlist far * far *x;    /* pointer last entry's link */
  234.   struct zlist far *z;  /* current zip entry structure */
  235.  
  236.   /* Initialize zip file info */
  237.   zipbeg = 0;
  238.   zfiles = NULL;                        /* Points to first header */
  239.   zcomlen = 0;                          /* zip file comment length */
  240.  
  241.   /* If zip file exists, read headers and check structure */
  242. #ifdef VMS
  243.   if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
  244.     return ZE_OK;
  245.   {
  246.     int rtype;
  247.     VMSmunch(zipfile, GET_RTYPE, (char *)&rtype);
  248.     if (rtype == FAT$C_VARIABLE) {
  249.       fprintf(stderr,
  250.      "\n     Error:  zipfile is in variable-length record format.  Please\n\
  251.